import { IS_IOS } from './ScreenUtil'
import { AppReg } from './RegUtil'
import { ImageHost } from 'src/config/index'
import store from '@store'

export const enableAndroidDoubleBackExit = false
export const logoutAvator = require('src/imgC/userinfo/camera.png')
export const unloginDefaultPortrait = require('src/imgC/userinfo/default_pic.png')
export const companyDefaultLogo = require('src/imgC/company/company-logo.png')
export const companyLogo = require('src/imgC/template/company-default-logo.png')
export const defaultPortraitGray = require('src/imgC/home/default_portrait_gray.png')
export const userAvatorLocalB = {
  male: require('src/imgC/company/company_avator_boy.png'),
  female: require('src/imgC/company/company_avator_girl.png')
}
export const userAvatorLocalC = {
  male: [
    require('src/imgC/userinfo/avator-boy1.png'),
    require('src/imgC/userinfo/avator-boy2.png'),
    require('src/imgC/userinfo/avator-boy3.png')
  ],
  female: [
    require('src/imgC/userinfo/avator-girl1.png'),
    require('src/imgC/userinfo/avator-girl2.png'),
    require('src/imgC/userinfo/avator-girl3.png')
  ]
}

export const getUserAvatorLocalB = (gender = 'male', userId) => {
  return userAvatorLocalB[gender]
}
export const getUserAvatorLocalC = (gender = 'male', userId = 0) => {
  const avators = userAvatorLocalC[gender] || userAvatorLocalC['male']
  return avators[userId % 3]
}
export const getUserAvatorLocalCByGender = (gender = 'male', userId = 0) => {
  gender =
    gender && (gender.toString() === '1' || gender.toString() === 'male')
      ? 'male'
      : 'female'
  const avators = userAvatorLocalC[gender] || userAvatorLocalC['male']
  return avators[userId % 3]
}
export const getGenderChinese = gender => {
  switch (gender) {
    case 'male':
      return '男'
    case 'female':
      return '女'
    default:
      return ''
  }
}

export const companyIconList = [
  require('src/imgC/company/company-bg1.png'),
  require('src/imgC/company/company-bg2.png'),
  require('src/imgC/company/company-bg3.png'),
  require('src/imgC/company/company-bg4.png'),
  require('src/imgC/company/company-bg5.png')
]
export const companyBgColoList = [
  require('src/imgC/company/company-bg1.png'),
  require('src/imgC/company/company-bg2.png'),
  require('src/imgC/company/company-bg4.png'),
  require('src/imgC/company/company-bg5.png')
]
export const companyBGList = [
  require('src/imgC/company/company-header1.png'),
  require('src/imgC/company/company-header2.png'),
  require('src/imgC/company/company-header3.png')
]

export const notifyIcon = {
  companyIcon: require('src/imgC/message/notify_icon_3.png'),
  famousCompanyIcon: require('src/imgC/message/notify_icon_5.png'),
  positionIcon: require('src/imgC/message/notify_icon_1.png'),
  memberIcon: require('src/imgC/message/notify_icon_2.png')
}
export const unitList = ['小时', '天', '周', '月', '次']
export const settlementMethod = [
  {
    name: '按天结算',
    short: '日结',
    unit: '元/天',
    unitDot: '元·天',
    value: 0
  },
  {
    name: '按周结算',
    short: '周结',
    unit: '元/周',
    unitDot: '元·周',
    value: 1
  },
  {
    name: '按月结算',
    short: '月结',
    unit: '元/月',
    unitDot: '元·月',
    value: 2
  },
  {
    name: '按次结算',
    short: '次结',
    unit: '元/次',
    unitDot: '元·次',
    value: 3
  }
]

export function getSettlementMethod(value) {
  return settlementMethod.find(e => e.value == value) || {}
}

export function getSettlementUnitByShort(short) {
  return settlementMethod.find(e => e.short === short) || {}
}
export const schoolPostStates = [
  '招聘中',
  '未上架',
  '未付款',
  '平台下架',
  '平台永久下架',
  '上架待审核',
  '上架申请拒绝'
]
export const jobStatusArr = [
  {
    label: '寻找工作',
    value: 1
  },
  {
    label: '考虑机会',
    value: 2
  }
]

export function getJobStatus(jobStatus) {
  if (!jobStatus) {
    return null
  }
  switch (parseInt(jobStatus)) {
    case 1:
      return '寻找工作'
    case 2:
      return '考虑机会'
    default:
      return null
  }
}

export const education = [
  {
    key: '中专',
    values: [10, 11, 12, 13],
    educationValue: 1,
    value: 10
  },
  {
    key: '专科',
    values: [20, 21, 22, 23],
    educationValue: 2,
    value: 20
  },
  {
    key: '本科',
    values: [30, 31, 32, 33, 34, 35],
    educationValue: 3,
    value: 30
  },
  {
    key: '硕士',
    values: [40, 41, 42, 43, 44, 45],
    educationValue: 4,
    value: 40
  },
  {
    key: '博士',
    values: [50, 51],
    educationValue: 5,
    value: 50
  }
]

/**
 * 根据学历级别获取标签
 * @param degreeLevel
 */
export function getDegreeLabelByLevel(degreeLevel) {
  if (!degreeLevel) return null
  const degreeItem = education.find(item => {
    return item.values.includes(degreeLevel)
  })
  return degreeItem ? degreeItem.key : null
}

/**
 * 根据标签获取学历级别
 * @param degreeLabel
 * @returns {number}
 */
export function getDegreeLevelByLabel(degreeLabel) {
  if (!degreeLabel) return null
  const degreeLevel = education.find(item => {
    return item.key === degreeLabel
  })
  return degreeLevel ? degreeLevel.value : null
}

export function postGetDegreeLevelByLabel(degreeLabel) {
  if (!degreeLabel) return null
  const degreeLevel = education.find(item => {
    return item.key === degreeLabel
  })
  return degreeLevel ? degreeLevel.educationValue : null
}

export function getEducation(value) {
  const temp = education.find(e => e.values.includes(value))
  if (temp) {
    return temp.key
  } else {
    return false
  }
}

export function getEducationByValue(value) {
  const temp = education.find(
    e => e.values.includes(value) || e.educationValue === value
  )
  if (temp) {
    return temp.key
  } else {
    return false
  }
}

export function getPostEducation(value) {
  const temp = education.find(e => e.educationValue === value)
  if (temp) {
    return temp.key
  } else {
    return false
  }
}

export const isFastImg = require('src/imgC/home/tag_fast.png')

export const workNatures = [
  {
    type: 0,
    value: '兼职',
    img: require('src/imgC/home/tag_jianzhi.png')
  },
  {
    type: 1,
    value: '全职',
    img: require('src/imgC/home/tag_quanzhi.png')
  },
  {
    type: 2,
    value: '实习',
    img: require('src/imgC/home/tag_jianzhi.png')
  }
]

export function getWorkNatureImgByType(type) {
  const typeInt = parseInt(type)
  const workNature = workNatures.find(item => {
    return item.type === typeInt
  })
  return workNature ? workNature.img : null
}

export const CityList = require('src/static/city.json')

export const isLegalCity = city => {
  return CityList.some(e => e.cityName === city)
}

// 方法定义 lat,lng
export const getDistance = (lat1, lng1, lat2, lng2) => {
  if (!lat1 || !lng1 || !lat2 || !lng2) {
    return ''
  }
  lat1 = Number(lat1)
  lng1 = Number(lng1)
  lat2 = Number(lat2)
  lng2 = Number(lng2)
  const radLat1 = (lat1 * Math.PI) / 180.0
  const radLat2 = (lat2 * Math.PI) / 180.0
  const a = radLat1 - radLat2
  const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0
  let s =
    2 *
    Math.asin(
      Math.sqrt(
        Math.pow(Math.sin(a / 2), 2) +
        Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)
      )
    )
  s = s * 6378.137 // EARTH_RADIUS;
  s = Math.round(s * 10000) / 10000
  s = s.toFixed(1)
  return s + 'km'
}

/* ios app关键字过滤 */
export const iosAppFilter = (data = [], keys = []) => {
  if (IS_IOS) {
    return data.filter(e => {
      const word = keys.reduce((a, b) => a[b] || '', e)
      return !(word && AppReg.test(word))
    })
  } else {
    return data
  }
}

export const fixImageUrl = (value = []) => {
  return value.map(e => ({
    url: e.url.startsWith('http') ? e.url : ImageHost + e.url
  }))
}
export const fixImageUrls = url => {
  if (url) {
    return url.startsWith('http') ? url : ImageHost + url
  } else {
    return ''
  }
}

export const isRoleB = () => {
  const {
    userinfo: { role }
  } = store.getState()
  return role === 'B'
}

export const getUserId = () => {
  const {
    userinfo: { userId }
  } = store.getState()
  return userId
}

export const getCompleteUserId = () => {
  const { userinfo } = store.getState()
  const { role } = userinfo
  if (role === 'B') {
    const {
      B: { userId }
    } = userinfo
    return userId
  } else {
    const {
      C: { userId }
    } = userinfo
    return userId
  }
}

export const BaseInfo = {
  notLogin: 1,
  baseInfoNotCompleted: 2,
  companyNotCompleted: 3,
  completed: 4
}
export const Role = {
  B: 'B',
  C: 'C'
}

/**
 * 检测用户的相关基本信息
 */
export function getUserBaseInfo() {
  const { userinfo, BMineCenterStore } = store.getState()
  const { role } = userinfo
  const roleTye = role === 'C' ? Role.C : Role.B
  const isRoleB = roleTye === Role.B
  const isRoleC = roleTye === Role.C
  const baseInfo = {
    isRoleB, // B端
    isRoleC, // C端
    role: roleTye, // 角色
    baseInfo: BaseInfo.completed // 基本信息
  }
  if (isRoleC) {
    // C端
    const {
      userId,
      C: { userId: userIdC }
    } = userinfo
    if (!userId) {
      baseInfo.baseInfo = BaseInfo.notLogin
    } else if (!userIdC) {
      baseInfo.baseInfo = BaseInfo.baseInfoNotCompleted
    }
  } else {
    // B端
    const {
      userId,
      B: { userId: userIdB }
    } = userinfo
    const {
      haveCompanyApply: { companyDone }
    } = BMineCenterStore
    if (!userId) {
      baseInfo.baseInfo = BaseInfo.notLogin
    } else if (!userIdB) {
      baseInfo.baseInfo = BaseInfo.baseInfoNotCompleted
    } else if (!companyDone) {
      // 未完成审核
      baseInfo.baseInfo = BaseInfo.companyNotCompleted
    }
  }
  return baseInfo
}
